package tech.aistar.Conteoller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.anno.FormToken;
import tech.aistar.mapper.*;
import tech.aistar.model.dto.AllJobCompanyDTO;
import tech.aistar.model.dto.Code;
import tech.aistar.model.dto.Result;
import tech.aistar.model.entity.*;
import tech.aistar.service.ICompanyService;
import tech.aistar.service.IResumeService;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("resume")
public class ResumeController {

    @Autowired
    private ApplyedMapper applyedMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IResumeService iResumeService;

    @Autowired
    private WantjobMapper wantjobMapper;

    @Autowired
    private ItemexperienceMapper itemexperienceMapper;

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private ICompanyService iCompanyService;


    @FormToken(repeat = true)
    @ResponseBody
    @GetMapping("applejob")
    public Result appleJob(String jId){
        Integer jobId= Integer.valueOf(jId);
        Customer customer = (Customer) redisTemplate.opsForValue().get("customer");
        Resume resume=iResumeService.findResumeByCustomerId(customer.getId());
        Applyed applyed=new Applyed(null,customer.getId(),jobId,resume.getId(),0,new java.util.Date());
        int state=applyedMapper.addApplyedJob(applyed);
        System.out.println(state);
        if(state==1){
            redisTemplate.convertAndSend("chart01","new");
            return new Result(Code.OK,"投递成功");
        }
        return new Result(Code.BAD,"投递失败");
    }

    @ResponseBody
    @GetMapping("personal")
    public Result personalResume(){
        Customer customer = (Customer) redisTemplate.opsForValue().get("customer");
        Resume resume=iResumeService.findResumeByCustomerId(customer.getId());
        return new Result(Code.OK,"查询成功",resume);
    }

    @ResponseBody
    @GetMapping("update")
    public Result alterResume(String phone,String img){
        Customer customer = (Customer) redisTemplate.opsForValue().get("customer");
        Resume resumes=iResumeService.findResumeByCustomerId(customer.getId());
        Resume resume=new Resume();
        resume.setId(resumes.getId());
        resume.setPhone(phone);
        resume.setImg(img);
        iResumeService.updateResume(resume);
        return new Result(Code.OK,"修改成功",iResumeService.findResumeByCustomerId(customer.getId()));
    }

    @ResponseBody
    @GetMapping("updatewantjob")
    public Result updateWantJob(String wantJobId,String jobName,String jobCity){
        Integer id= Integer.valueOf(wantJobId);
        System.out.println(jobName+"mmmmmmmmmmmmmmmm"+jobCity);
        Wantjob wantjob=new Wantjob();
        wantjob.setId(id);
        wantjob.setJob(jobName);
        wantjob.setCity(jobCity);
        wantjobMapper.updateWantJob(wantjob);
        return new Result(Code.OK,"修改成功",wantjobMapper.getWantjobByWantjobId(id));
    }

    @ResponseBody
    @GetMapping("selectwantjob")
    public Result selectWantJob(String wantJobId){
        System.out.println(wantJobId);
        Integer id= Integer.valueOf(wantJobId);
        Wantjob wantjob=wantjobMapper.getWantjobByWantjobId(id);
        System.out.println("jjijijikokokokookok"+wantjob);
        return new Result(Code.OK,"修改成功",wantjob);
    }


    @ResponseBody
    @GetMapping("selectitems")
    public Result selectItems(){
        Customer customer = (Customer) redisTemplate.opsForValue().get("customer");
        Resume resumes=iResumeService.findResumeByCustomerId(customer.getId());
        List<Itemexperience> list=itemexperienceMapper.getItemExperByResumeId(resumes.getId());
        return new Result(Code.OK,"修改成功",list);
    }


    @ResponseBody
    @GetMapping("delitem")
    public Result deltItem(String id){
        Integer itemId= Integer.valueOf(id);
        itemexperienceMapper.delItemexperience(itemId);
        return new Result(Code.OK,"删除成功");
    }


    @ResponseBody
    @GetMapping("uodateitem")
    public Result updateItem(String id){
        Integer itemId= Integer.valueOf(id);
        itemexperienceMapper.delItemexperience(itemId);
        return new Result(Code.OK,"删除成功");
    }



    @ResponseBody
    @GetMapping("myapplyed")
    public Result myApplyed(){
        Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
        List<Applyed> applyeds=applyedMapper.getByCID(customer.getId());
        List<Integer> ids=new ArrayList<>();
        for (int i=0;i<applyeds.size();i++){
            ids.add(applyeds.get(i).getJobId());
        }
        List<Job> jobs=jobMapper.getByIds(ids);
        List<Company> companies=iCompanyService.findAllCompany();
        AllJobCompanyDTO allJobCompanyDTO=new AllJobCompanyDTO();
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setCompanyList(companies);
        return new Result(Code.OK,"查询成功",allJobCompanyDTO);
    }






}
